package com.hyt.it.ogt.kq.service.gov.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hyt.it.ogt.kq.service.admission.IAdmissionTicketInfoService;
import com.hyt.it.ogt.kq.service.admission.IExamineeInfoService;
import com.hyt.it.ogt.kq.service.gov.feign.ks.model.AdmissionConsumeEnum;
import com.hyt.it.ogt.kq.service.gov.feign.ks.model.AdmissionConsumeVO;
import com.hyt.it.ogt.kq.service.gov.feign.ks.model.KwExamCandidateVO;
import com.hyt.it.ogt.kq.service.gov.feign.ks.model.KwExamVO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 
 *
 * @author zhoumiao
 * @since 2023/3/2 13:51
 **/
@Component
@Slf4j
public class AdmissionConsumer {
    @Resource
    private IAdmissionTicketInfoService iAdmissionTicketInfoService;
    @Resource
    private IExamineeInfoService iExamineeInfoService;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "${mq.admission.ks.queue.examCandidate}",
            autoDelete = "false", durable = "true"),
            exchange = @Exchange(value = "${mq.admission.ks.exchange}", type = ExchangeTypes.DIRECT),
            key = {"${mq.admission.ks.routingKey.examCandidate}"})})
    public void consume(String json, Message message, Channel channel) throws IOException {
        try {
            log.info("[准考证][无考场][考生数据同步]消费数据信息更新 {} 队列消息：{}", channel, json);
            AdmissionConsumeVO admissionConsumeVO = JSON.parseObject(json, AdmissionConsumeVO.class);
            AdmissionConsumeEnum admissionConsumeEnum = AdmissionConsumeEnum.admissionConsumeEnum(admissionConsumeVO.getType());
            String content = admissionConsumeVO.getContent();
            switch (admissionConsumeEnum) {
                case CANDIDATE_INSERT:
                    List<KwExamCandidateVO> kwExamCandidateVOS = JSONArray.parseArray(content, KwExamCandidateVO.class);
                    for (KwExamCandidateVO kwExamCandidateVO : kwExamCandidateVOS) {
                        iExamineeInfoService.insertByKw(kwExamCandidateVO, "mq");
                    }
                    break;
                case CANDIDATE_DELETE:
                    kwExamCandidateVOS = JSONArray.parseArray(content, KwExamCandidateVO.class);
                    for (KwExamCandidateVO kwExamCandidateVO : kwExamCandidateVOS) {
                        iExamineeInfoService.deleteByKw(kwExamCandidateVO, "mq");
                    }
                    break;
                case CANDIDATE_UPDATE:
                    kwExamCandidateVOS = JSONArray.parseArray(content, KwExamCandidateVO.class);
                    for (KwExamCandidateVO kwExamCandidateVO : kwExamCandidateVOS) {
                        iExamineeInfoService.updateByKw(kwExamCandidateVO, "mq");
                    }
                    break;
                case EXAM_UPDATE:
                    iAdmissionTicketInfoService.updateKwExam(JSON.parseObject(content, KwExamVO.class), "mq");
                    break;
                default:
                    log.warn("[准考证][无考场][考生数据同步]消费无考场准考证类型不存在 {}", admissionConsumeVO.getType());
            }
        } catch (Exception e) {
            log.error("[准考证][无考场][考生数据同步]消费无考场准考证报错 ", e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
